﻿@inject IUserConfigService configService

@inherits LayoutComponentBase

<RadzenSplitter Collapse="@OnCollapse" Expand="@OnExpand" Resize="@OnResize">
    <RadzenSplitterPane Size="@SideBarSize" Collapsed="@Collapsed" Min="150px" Max="70%">
        @LeftPane
    </RadzenSplitterPane>
    <RadzenSplitterPane Collapsible="false">
        @RightPane
    </RadzenSplitterPane>
</RadzenSplitter>

@code{

    [Parameter] public RenderFragment LeftPane { get; set; }

    [Parameter] public RenderFragment RightPane { get; set; }

    private string SideBarSize { get; set; }
    private bool Collapsed { get; set; }

    protected override void OnInitialized()
    {
        base.OnInitialized();

        SideBarSize = configService.Get(ConfigSettings.SideBarWidth, "20%");
        Collapsed = configService.GetBool(ConfigSettings.SideBarCollapsed);
    }

    async Task OnResize(RadzenSplitterResizeEventArgs args)
    {
        if( args.PaneIndex == 0 )
        {
            var newSize = $"{(int)args.NewSize}%";
            await configService.SetForUser(ConfigSettings.SideBarWidth, newSize);
            await configService.SetForUser(ConfigSettings.SideBarCollapsed, "false");
        }
    }

    async Task OnCollapse(RadzenSplitterEventArgs args)
    {
        if( args.PaneIndex == 0 )
        {
            Collapsed = true;
            await configService.SetForUser(ConfigSettings.SideBarCollapsed, Collapsed.ToString());
        }
    }

    async Task OnExpand(RadzenSplitterEventArgs args)
    {
        if( args.PaneIndex == 0 )
        {
            Collapsed = false;
            await configService.SetForUser(ConfigSettings.SideBarCollapsed, Collapsed.ToString());
        }
    }

}